import * as Common from '@/config/common.js' //引入common
import md5 from 'js-md5'

function request(url, param, way, success, fail, complete, isload = true) {

	var strEncrypt = Common.Encrypt(JSON.stringify(param))

	var isLogin = uni.getStorageSync('isLogin');
	var header = {};

	if (isLogin) {
		var userInfo = uni.getStorageSync('userInfo');
		header['LOGIN_ID'] = md5(Common.sys.key + userInfo.username).toUpperCase();
		if (Common.sys.toSSOLogin) {
			header['LOGIN_PWD'] = userInfo.password;
		} else {
			header['LOGIN_PWD'] = md5(Common.sys.key + userInfo.password).toUpperCase();
		}
	}
	// #ifdef H5
	header['SOURCES'] = 3;
	// #endif
	// #ifdef APP-PLUS
	header['SOURCES'] = 2;
	// #endif
	// #ifdef H5
	if (isload) {
		uni.showLoading({
			mask: true,
			title: "请稍后..."
		});
	}
	// #endif
	uni.showNavigationBarLoading();

	uni.request({
		//url:'http://192.168.5.109:8880/YdjyBeianServer/api/v1.0/'+url,
		url: Common.sys.baseUrl + 'api/v1.0/' + url,
		method: way,
		data: {
			'data': strEncrypt
		},
		header: header,
		fail: function(err) {
			uni.showToast({
				title: '请稍后重试',
				image: '../../static/image/error.png'
			})
			fail && fail(err);
		},
		success: function(res) {
			// console.log(res.data)
			if (typeof res.data === 'string') {
				var content = Common.Decrypt(res.data);
				var result = JSON.parse(content);
				var data = {
					'data': result
				}
				success(data);
			} else {
				success(res);
			}

		},
		complete: function(msg) {
			// console.log('33333333333333',msg)
			uni.hideNavigationBarLoading();
			// #ifdef H5
			if (isload) {
				uni.hideLoading();
			}
			// #endif
			complete && complete(msg);
		}
	})
}

export default request
